﻿Imports System.IO
Imports Microsoft.Win32
Imports System.ComponentModel
Imports System.Configuration.Install

Public Class DeviceDeploymentInstallerActions

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add initialization code after the call to InitializeComponent

    End Sub

    Private Shared Function CEAppMgrApplicationDirectory() As String
        Dim activeSyncKey As RegistryKey = _
             Registry.LocalMachine.OpenSubKey(My.Resources.ActiveSync_Registry_Key)
        Dim activeSyncPath As String = _
       CStr(activeSyncKey.GetValue(My.Resources.CEAppMgr_Install_Dir_Registry_Key))
        Dim installPath As String = activeSyncPath + _
                                                 My.Resources.Application_Directory
        activeSyncKey.Close()
        Return installPath
    End Function

    Private Shared Function CEAppMgrExecutable() As String
        Dim appMgrKey As RegistryKey = _
                      Registry.LocalMachine.OpenSubKey(My.Resources.CEApp_Mgr_Path)
        Dim appMgrPath As String = CStr(appMgrKey.GetValue(Nothing))
        appMgrKey.Close()
        Return appMgrPath
    End Function

    Private Sub ActionBeforeInstall(ByVal sender As Object, _
                                    ByVal e As InstallEventArgs) _
                                                           Handles Me.BeforeInstall
        'Debugger.Break()
        MsgBox("Here")

        'Copy installation files to the CEAppMgr application directory
        Dim installPath As String = CEAppMgrApplicationDirectory()
        Dim tmpPath As String = (New FileInfo(Me.Context.Parameters.Item _
                   (My.Resources.Installation_Context_Assembly_Key))).DirectoryName
        My.Computer.FileSystem.CopyDirectory(tmpPath, installPath, True)

        'Run CEAppMgr to install the application on the device
        Dim iniFile As String = """" + Path.Combine(installPath, _
                                   My.Resources.Application_Cab_INI_File) + " "" "
        System.Diagnostics.Process.Start(CEAppMgrExecutable, inifile)
    End Sub

    Public Sub ActionBeforeUninstall(ByVal sender As Object, _
                                     ByVal e As InstallEventArgs) _
                                                         Handles Me.BeforeUninstall
        'Remove the installation files we manually copied
        My.Computer.FileSystem.DeleteDirectory(CEAppMgrApplicationDirectory, _
                                    FileIO.DeleteDirectoryOption.DeleteAllContents)
    End Sub
End Class


